﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>用户协议</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link href="/lobsteruiframe/layuiadmin/layui/css/layui.css" rel="stylesheet" />
    <link href="/lobsteruiframe/layuiadmin/style/admin.css" rel="stylesheet" />
    <script src="/lobsteruiframe/layuiadmin/layui/layui.js"></script>
    <script src="/jslib/jquery-3.4.1.min.js"></script>
    <script src="/jslib/lobster.js"></script>

    <script src="/lobsteruiframe/ueditor/ueditor.config.js"></script>
    <script src="/lobsteruiframe/ueditor/ueditor.all.js"></script>
</head>
<body>
    <div class="layui-fluid">
        <form class="layui-form">
            <div class="layui-card">
                <div class="layui-card-header layui-form layuiadmin-card-header-auto layui-clear">
                    <div class="layui-inline">
                        <div class="layui-form-item">
                            <label class="layui-form-label">医疗机构：</label>
                            <div class="layui-input-block">
                                <select name="workId" id="workId" lay-filter="workId"></select>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-card-body">
    
                    <table class="layui-table" id="griduserAgreementType" lay-filter="griduserAgreementType"></table>
    
                    <script type="text/html" id="barRowAgreement">
                        <a class="layui-btn layui-btn-xs" lay-event="alter"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                        {{# if(d.IsOpen==0){ }}
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="disable"><i class="layui-icon layui-extend-stop"></i>停用</a>
                        {{# } else { }}
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="enable"><i class="layui-icon layui-icon-ok"></i>启用</a>
                        {{# } }}
                    </script>
                    <script type="text/html" id="barRowIsOpen">
                        {{# if(d.IsOpen==1){ }}
                        <span style="color:red">{{d.Status}}</span>
                        {{# } else { }}
                        <span>{{d.Status}}</span>
                        {{# } }}
                    </script>
                    <script type="text/html" id="toolbar">
                        <a class="layui-btn layui-btn-sm" lay-event="add-submit"><i class="layui-icon layui-icon-add-1"></i>添加</a>
                    </script>
                </div>
            </div>
        </form>
    </div>
</body>
</html>

<script type="text/javascript">
    lobsterlayui.main(['index', 'layer', 'form', 'element', 'treeSelect', 'table', 'laydate', 'upload', 'tree'], {
        data: {
            $layui: null,
            right: null,
            tableins: null,
            base64EncodeChars: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
            base64DecodeChars: new Array(
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
                52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
                -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
                15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
                -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
                41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1),
        },
        pageload: function (right) {
            this.data.right = right;
            this.loadselectworker(() => {
                this.getdata();
            });

            this.initevent();
        },
        initevent() {
            var self = this;

            // 监听行工具事件
            layui.table.on("tool(griduserAgreementType)", function (obj) {
                var data = obj.data;
                var tr = obj.tr;
                if (obj.event === "disable" || obj.event === "enable") {
                    // 启用停用消息类型
                    var confirm_msg = "";
                    var status = 0;
                    if (data.IsOpen == 0) {
                        confirm_msg = "确认停用么?";
                        status = 1;
                    }
                    else {
                        confirm_msg = "确认启用么?";
                        status = 0;
                    }

                    layer.confirm(confirm_msg, { icon: 3, title: "提示" },
                        function (index) {
                            lobsterlayui.GET('/admin/v1/worker/StopAgreement',{Id:data.Id,status:status}).then(retdata=>{
                                layer.close(index); //关闭弹层
                                layer.msg("保存成功");
                                self.data.tableins.reload();// 数据刷新
                            })
                        });
                } else if (obj.event === "alter") {
                    // 修改用户协议
                    var index = layer.open({
                        type: 1,
                        title: "编辑用户协议",
                        btn: ["保存", "取消"],
                        btnAlign: "r",
                        content: $('#dlg_addform').html(),
                        area: ["800px", "80%"],
                        success: function (layero, index) {
                            $("#LAY-form-reset").trigger('click');
                            layui.form.val('layuiadmin-form-agreement',data);

                            window.agreementform.init(data);
                        },
                        yes: function (index, layero) {

                            var submit = $("#btnSubmit");
                            //监听提交
                            layui.form.on("submit(btnSubmit)", function (formdata) {
                                var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                                var field=formdata.field;
                                // field.Contents = self.base64encode(self.utf16to8(field.Contents));
                                lobsterlayui.POST('/admin/v1/worker/SaveBasicAgreement',field).then(retdata=>{
                                    layer.close(load);
                                    layer.close(index);
                                    layer.msg("保存成功");
                                    self.data.tableins.reload(); //数据刷新
                                })                              
                            });
                            submit.trigger("click");
                        },
                        btn2: function (index, layero) {
                            tr.removeAttr("class");
                            window.agreementform.free();
                        },
                        cancel: function () {
                            tr.removeAttr("class");
                            window.agreementform.free();
                        }
                    });

                }
            });

            // 点击添加用户协议
            layui.table.on("toolbar(griduserAgreementType)", function (obj) {
                if (obj.event == "add-submit") {
                    layer.open({
                        type: 1,
                        title: "添加用户协议",
                        btn: ["保存", "取消"],
                        btnAlign: "r",
                        content: $('#dlg_addform').html(),
                        area: ["800px", "80%"],
                        success: function (layero, index) {
                            $("#LAY-form-reset").trigger('click');
                            $('#greementid').val(0);
                            $('#greementworkid').val($("#workId").val());

                            window.agreementform.init();
                        },
                        yes: function (index, layero) {
                            var submit = $("#btnSubmit");
                            //监听提交
                            layui.form.on("submit(btnSubmit)", function (formdata) {
                                var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                                var field=formdata.field;
                                // field.Contents = self.base64encode(self.utf16to8(field.Contents));
                                lobsterlayui.POST('/admin/v1/worker/SaveBasicAgreement',field).then(retdata=>{
                                    layer.close(load);
                                    layer.close(index);
                                    layer.msg("保存成功");
                                    self.data.tableins.reload(); //数据刷新
                                })                              
                            });
                            submit.trigger("click");
                        },
                        btn2: function (index, layero) {
                            // tr.removeAttr("class");
                            window.agreementform.free();
                        },
                        cancel: function () {
                            // tr.removeAttr("class");
                            window.agreementform.free();
                        }
                    });
                }
            });
        },
        loadselectworker(callback) {
            //获取机构数据
            lobsterlayui.GET('/admin/v1/worker/GetSelectWorkerData').then(data => {
                //医疗机构
                var workers = data["Workers"];
                $("#workId").html("");
                $.each(workers, function (i, item) {
                    $("#workId").append("<option value=\"" + item.WorkId + "\">" + item.WorkName + "</option>");
                });

                $('#workId').val(workers[0].WorkId);//默认选择当前登录机构
                //重新渲染
                layui.form.render("select");

                if (callback) {
                    callback();
                }
            })

            layui.form.on('select(workId)', function (data) {
                if (callback) {
                    callback();
                }
            });
        },
        getdata: function () {
            var self = this;
            self.data.tableins = layui.table.render({
                elem: "#griduserAgreementType",
                headers: lobsterlayui.GetHeaderData(),
                where: {
                    workId: $("#workId").val()
                },
                height: "full-140",
                cellMinWidth: 80, //全局定义常规单元格的最小宽度
                toolbar: '#toolbar',
                url: "/admin/v1/worker/GetAgreementList", //数据接口
                page: true, //开启分页
                loading: true,
                cols: [[ //表头
                    { field: "Id", width: "10%", title: "ID" },
                    { field: "AgreementName", width: "20%", title: "协议名称" },
                    { field: "AgreementCode", width: "16%", title: "协议编号" },
                    { field: "Title", width: "30%", title: "标题" },
                    { field: "Status", width: "10%", title: "状态", templet: "#barRowIsOpen" },
                    { title: "操作", width: "14%", align: 'center', toolbar: "#barRowAgreement" }
                ]],
                parseData: function (res) {
                    return {
                        code: res.code,
                        msg: res.msg,
                        count: res.data.count,
                        data: res.data.data
                    }
                }
            });
        },
        base64encode: function (str) {
            var self = this;
            var out, i, len;
            var c1, c2, c3;
            len = str.length;
            i = 0;
            out = "";
            while (i < len) {
                c1 = str.charCodeAt(i++) & 0xff;
                if (i == len) {
                    out += self.data.base64EncodeChars.charAt(c1 >> 2);
                    out += self.data.base64EncodeChars.charAt((c1 & 0x3) << 4);
                    out += "==";
                    break;
                }
                c2 = str.charCodeAt(i++);
                if (i == len) {
                    out += self.data.base64EncodeChars.charAt(c1 >> 2);
                    out += self.data.base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
                    out += self.data.base64EncodeChars.charAt((c2 & 0xF) << 2);
                    out += "=";
                    break;
                }
                c3 = str.charCodeAt(i++);
                out += self.data.base64EncodeChars.charAt(c1 >> 2);
                out += self.data.base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
                out += self.data.base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
                out += self.data.base64EncodeChars.charAt(c3 & 0x3F);
            }
            return out;
        },
        //解码的方法
        base64decode: function (str) {
            var self = this;
            var c1, c2, c3, c4;
            var i, len, out;
            len = str.length;
            i = 0;
            out = "";
            while (i < len) {
                do {
                    c1 = self.data.base64DecodeChars[str.charCodeAt(i++) & 0xff];
                } while (i < len && c1 == -1);
                if (c1 == -1)
                    break;
                do {
                    c2 = self.data.base64DecodeChars[str.charCodeAt(i++) & 0xff];
                } while (i < len && c2 == -1);
                if (c2 == -1)
                    break;
                out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
                do {
                    c3 = str.charCodeAt(i++) & 0xff;
                    if (c3 == 61)
                        return out;
                    c3 = self.data.base64DecodeChars[c3];
                } while (i < len && c3 == -1);
                if (c3 == -1)
                    break;
                out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
                do {
                    c4 = str.charCodeAt(i++) & 0xff;
                    if (c4 == 61)
                        return out;
                    c4 = self.data.base64DecodeChars[c4];
                } while (i < len && c4 == -1);
                if (c4 == -1)
                    break;
                out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
            }
            return out;
        },
        utf16to8: function (str) {
            var out, i, len, c;
            out = "";
            len = str.length;
            for (i = 0; i < len; i++) {
                c = str.charCodeAt(i);
                if ((c >= 0x0001) && (c <= 0x007F)) {
                    out += str.charAt(i);
                } else if (c > 0x07FF) {
                    out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
                    out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
                    out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
                } else {
                    out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
                    out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
                }
            }
            return out;
        },
        utf8to16: function (str) {
            var out, i, len, c;
            var char2, char3;
            out = "";
            len = str.length;
            i = 0;
            while (i < len) {
                c = str.charCodeAt(i++);
                switch (c >> 4) {
                    case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                        // 0xxxxxxx
                        out += str.charAt(i - 1);
                        break;
                    case 12: case 13:
                        // 110x xxxx  10xx xxxx
                        char2 = str.charCodeAt(i++);
                        out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
                        break;
                    case 14:
                        // 1110 xxxx 10xx xxxx 10xx xxxx
                        char2 = str.charCodeAt(i++);
                        char3 = str.charCodeAt(i++);
                        out += String.fromCharCode(((c & 0x0F) << 12) |
                            ((char2 & 0x3F) << 6) |
                            ((char3 & 0x3F) << 0));
                        break;
                }
            }
            return out;
        },
    })

    window.agreementform={
        init(data){
            UE.delEditor('editor');
            var ue = UE.getEditor('editor');
            ue.ready(function () {
                if (data) {
                    ue.setContent(data.Contents);
                }
                ue.execCommand('serverparam', {
                    'workId': '1',
                    'appkey': '99999999999999999991',
                });
            });
        },
        free(){
            UE.delEditor('editor');
        }
    }
</script>

<script type="text/html" id="dlg_addform">
    <form class="layui-form" lay-filter="layuiadmin-form-agreement" id="layuiadmin-form-agreement" style="padding: 20px 30px 0 0;">
        <div class="layui-form-item">
            <label class="layui-form-label"><i style="color:red;">*&ensp;</i>协议名称</label>
            <div class="layui-input-block">
                <input type="text" name="AgreementName" id="agreementname" lay-verify="required" maxlength="20" placeholder="请输入协议名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><i style="color:red;">*&ensp;</i>协议编号</label>
            <div class="layui-input-block">
                <input type="text" name="AgreementCode" id="agreecode" lay-verify="required" maxlength="20" placeholder="请输入协议编号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><i style="color:red;">*&ensp;</i>协议标题</label>
            <div class="layui-input-block">
                <input type="text" name="Title" lay-verify="required" placeholder="请输入协议标题" maxlength="30" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><i style="color:red;">*&ensp;</i>协议摘要</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" name="Abstract" placeholder="请输入协议摘要" maxlength="200"></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">协议内容</label>
            <div class="layui-input-block">
                <div id="editor" name="Contents" type="text/plain" style="width:643px;height:200px;">
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-hide">
            <input type="text" name="WorkId" id="greementworkid">
            <input type="text" name="Id" id="greementid" value="0">   
            <!-- <input type="text" name="Contents" id="content"> -->
        </div>
        <div class="layui-form-item layui-hide">
            <input type="button" lay-submit lay-filter="btnSubmit" id="btnSubmit" value="确认">
            <input type="reset" id="LAY-form-reset" value="重置">
        </div>

    </form>
</script>